package com.glasses.business.fragment;

import android.content.Intent;
import android.view.View;

import com.glasses.business.activity.OrderDetailActivity;
import com.glasses.business.adapter.OrderAdapter;
import com.glasses.business.base.BaseListFragment;
import com.glasses.business.base.MyBaseAdapter;
import com.glasses.business.entity.NewsList;
import com.glasses.business.entity.OrderList;
import com.glasses.business.event.BussEvent;
import com.glasses.business.http.NetUtils;
import com.glasses.business.inter.OnItemClick;
import com.glasses.business.utils.Tools;

import net.neiquan.okhttp.NetCallBack;
import net.neiquan.okhttp.ResultModel;

import org.haitao.common.utils.AppLog;
import org.haitao.common.utils.ToastUtil;

import java.util.List;

import butterknife.ButterKnife;
import de.greenrobot.event.EventBus;

/**
 * Author：邓勇军
 * English Name：Deng,The Sky Fucker!
 * Email：dandycoder@126.com
 * Date：2017/3/7
 * Version：1.0
 */

public class OrderListFragment extends BaseListFragment {
    private int status;
    private List<OrderList> list;
    private OrderAdapter orderAdapter;

    @Override
    protected boolean isHaveHead() {
        return false;
    }

    @Override
    protected void setHeadViews() {
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
    }

    public void onEventMainThread(BussEvent event) {
        if (event.getState() == BussEvent.Order_Refush) {
            AppLog.e("==================Order_Refush=====================");
            onRefresh();
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        onRefresh();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.reset(this);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Override
    protected MyBaseAdapter getAdapter() {
        orderAdapter = new OrderAdapter(getActivity(), null);
        orderAdapter.setOnItemClick(new OnItemClick() {
            @Override
            public void onItemClick(int type, Object obj) {
                OrderList orderList = (OrderList) obj;
                Intent intent = new Intent(getActivity(), OrderDetailActivity.class);
                intent.putExtra("id", orderList.getId());
                startActivity(intent);
            }
        });
        return orderAdapter;
    }

    @Override
    protected int getLineNum() {
        return 0;
    }

    @Override
    public void initData() {
        if (getArguments() != null) {
            status = getArguments().getInt("status");
            loadDatas(true);
        }
    }

    private void loadDatas(final boolean isRefush) {
        Tools.showDialog(getActivity());
        NetUtils.getInstance().orderList(pageNum, PAEG_SIZE, status, new NetCallBack() {
            @Override
            public void onSuccess(String response, String mssage, ResultModel model) {
                Tools.dismissWaitDialog();
                mRecyclerview.loadMoreComplete();
                mRecyclerview.refreshComplete();
                mRefeshLy.hideAll();
                list = (List<OrderList>) model.getModelList();
                if (isRefush) {
                    orderAdapter.clear();
                }
                orderAdapter.append(list);
                if (list == null || list.size() < 10) {
                    if (isRefush && (list == null || list.size() == 0)) {
//                        mRefeshLy.showEmptyView();
                        mRefeshLy.showFailView();
                    }
                    mRecyclerview.setLoadingMoreEnabled(false);
                } else {
                    mRecyclerview.setLoadingMoreEnabled(true);
                }
            }

            @Override
            public void onFail(boolean dataError, int errorCode, String mssage) {
                Tools.dismissWaitDialog();
                ToastUtil.shortShowToast(mssage);
                //                mRefeshLy.hideAll();
                mRefeshLy.showFailView();
                mRecyclerview.loadMoreComplete();
                mRecyclerview.refreshComplete();
            }
        }, OrderList.class);
    }


    @Override
    public boolean mainTabShow() {
        return false;
    }

    @Override
    public void onRefresh() {
        pageNum = 0;
        loadDatas(true);
    }

    @Override
    public void onLoadMore() {
        pageNum++;
        loadDatas(false);
    }

    @Override
    public void onRetryClick() {
        pageNum = 0;
        loadDatas(true);
    }
}
